package Jets.loader
{
	import flash.display.Loader;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.setTimeout;

	public class JLoaderBase extends EventDispatcher
	{
		private var ms_nLoader:* = null;
		private var ms_nCallFun:Function = null;
		public function JLoaderBase(type:int,fun:Function)
		{
			super();
			ms_nCallFun = fun;
			switch(type)
			{
				case JLoaderType.Load_Type_Txt:
				case JLoaderType.Load_Type_Xml:
				case JLoaderType.Load_Type_BinFile:
					ms_nLoader = new URLLoader();
					ms_nLoader.addEventListener(Event.COMPLETE,onLoadCompleted);
					ms_nLoader.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
					break;
				case JLoaderType.Load_Type_Image:
					ms_nLoader = new Loader();
					ms_nLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,onLoadCompleted);
					ms_nLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
					break;
				case JLoaderType.Load_Type_Swf:
					ms_nLoader = new Loader();
					ms_nLoader.contentLoaderInfo.addEventListener(Event.INIT,onLoadCompleted);
					ms_nLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,onLoadError);
					break
				default:
					throw new Error("加载的资源类型不存在定义,查看JLoaderType")
					return;
			}
		}
		
		/**
		 * 文件下载的方式
		 * URLLoaderDataFormat定义
		 */
		public function set dataFormat(value:String):void
		{
			ms_nLoader.dataFormat = value;
		}
		
		private function onLoadCompleted(event:Event):void
		{
			this.dispatchEvent(event);
		}
		
		private function onLoadError(err:IOErrorEvent):void
		{
			trace(err.text);
		}
		
		public function load(url:URLRequest):void
		{
			ms_nLoader.load(url);
		}
		
		public function get callFun():Function
		{
			return ms_nCallFun;
		}
		public function get loader():*
		{
			return ms_nLoader;
		}
	}
}